import { Component, OnInit, Input } from '@angular/core';
import { MdlSnackbarService } from '@angular-mdl/core';
import { ConfigService } from './cfg.service';

@Component({
    selector: 'fields',
    templateUrl: './fields.component.html',
    styleUrls: ['./fields.component.scss'],
    providers: []
})
export class FieldsComponent implements OnInit {
    fields: any[] = [];
    currentTaskListId: number = 0;
    cols: any[] = [
        { header: '序号', field: 'index', style: { 'width': '80px', 'text-align': 'center', 'font-weight': 600 } },
        { header: '字段名称', field: 'columnName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '显示名称', field: 'fieldName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '显示/隐藏', field: 'visibleFlag', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '数据类型', field: 'fieldTypeName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '格式化', field: 'fieldFormat', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '字典转换', field: 'fieldEnumFormat', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '列宽', field: 'fieldWidth', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '筛选条件', field: 'searchFlag', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '筛选字典', field: 'searchEnum', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '栏目编号', field: 'taskListId', style: { 'display': 'none', 'overflow': 'hidden' } },
        { header: '排序', field: 'displayOrder', style: { 'width': '80px', 'text-align': 'center' } }
    ];

    @Input()
    bizId: number = 0;

    @Input()
    systemUrl: string = '';

    @Input()
    set taskListId(taskListId: number) {
        this.currentTaskListId = taskListId;
        if (!taskListId || !this.systemUrl) return;
        this.configService.showTaskListFields(taskListId, this.systemUrl).subscribe(fields => this.fields = fields)
    }

    get taskListId(): number { return this.currentTaskListId; }

    

    constructor(
        private configService: ConfigService,
        private mdlSnackbarService: MdlSnackbarService) {
    }
    ngOnInit() {
    }

    saveUpdate() {
        this.fields.forEach(field => {
            field.searchFlag = field.searchFlag ? 1 : 0;
            field.visibleFlag = field.visibleFlag ? 1 : 0;
        });
        this.configService.updateTaskListFields(this.fields, this.systemUrl).subscribe(data => this.mdlSnackbarService.showToast("字段保存成功！"));
    }
    refresh(){
        this.configService.showTaskListFields(this.taskListId, this.systemUrl).subscribe(fields => this.fields = fields)
    }
}
